﻿using FanaticRock.AudioManage.Helpers;
using FanaticRock.AudioManage.Models;
using FanaticRock.Core.DI;
using FanaticRock.Core.Helpers;
using FanaticRock.Core.Models;
using FanaticRock.Core.ServiceInterfaces;
using FanaticRock.Helpers;

namespace FanaticRock.TestDataSource
{
    [InjectMe(typeof(ISongProviderService))]
    public class TestSongProviderService : ISongProviderService
    {

        private ICacheStorageService _cacheStorageService;
        private ILocalDbService _localDbService;

        public TestSongProviderService(
            ICacheStorageService cacheStorageService,
            ILocalDbService localDbService)
        {
            _cacheStorageService = cacheStorageService;
            _localDbService = localDbService;
        }

        ///<summary>获得播放列表的曲子。</summary>
        public Song[] GetSongs()
        {
            var songs = _localDbService.Query<SongInPlayList>().GetAwaiter().GetResult();
            return songs.Where(s => System.IO.File.Exists(s.Uri.LocalPath)).OrderBy(s => s.PlayIndex).ToArray();
        }

        public Song[] GetSongsOld()
        {
            string[] fanbalDirPaths = [
                //@"E:\Program Files (x86)\DownKyi-1.5.9\Media",
                //@"D:\CloudMusic"
                ];

            var provider = new FanbalLocalMachineStorageProvider();

            var files = provider.GetFiles(fanbalDirPaths);

            var songList = new List<Song>();

            if (files != null)
            {
                foreach (var file in files)
                {
                    var song = CreateSong(file);
                    songList.Add(song);
                }
            }

            return songList.ToArray();
        }

        private Song CreateSong(string path)
        {
            var name = Path.GetFileNameWithoutExtension(path);
            var metadata = AudioFormatHelper.GetMetadata(path);

            var song = new Song()
            {
                Id = metadata.Id,
                Duration = metadata.Duration,
                Title = metadata.Name,
                Uri = UriHelper.GetUri(path),
            };

            return song;
        }
    }
}
